package com.wp.controller.impl;
import com.wp.entity.SysRole;
import com.wp.service.SysRoleService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/sys/role")
public class SysRoleController {

    // 注入属性
    @Resource
    private SysRoleService sysRoleService;

    // 遍历查询
    @GetMapping("/list")
    public String findAll(Model model) throws Exception {
        List<SysRole> roleList = sysRoleService.findAllSysRole();

        model.addAttribute("roleList", roleList); // 存储信息

        return "sysRole/list";
    }

    // 跳转添加页面
    @RequestMapping("/toAdd")
    public String toAdd() {

        return "sysRole/add";
    }


    @GetMapping("/codeExist")
    @ResponseBody
    public String codeExist(String code){

        SysRole sysRole = sysRoleService.codeExist(code);

        if (sysRole!=null){
            return "{\"exist\":\"1\"}";
        } else {
            return "{\"exist\":\"0\"}";
        }
    }

    // 添加信息
    @PostMapping("/add")
    public String add(SysRole sysRole) {

        int add = sysRoleService.toAdd(sysRole);

        if (add>0){
            return "redirect:/sys/role/list";
        }else {
            return null;
        }
    }
    // 删除信息
    @DeleteMapping("/del/{supId}")
    @ResponseBody // 页面不跳转》》用于异步请求
    public String del(@PathVariable (name = "supId") Integer id) {

        int del = sysRoleService.del(id);

        if (del>0){
            return "{\"delResult\":\"true\"}";
        }else {
            return "{\"delResult\":\"false\"}";
        }
    }

    // 查询个人信息
    @RequestMapping("/toUpdate/{supId}")
    public String toUpdate(Model model,@PathVariable(name = "supId")Integer id) {

        SysRole sysRole = sysRoleService.findById(id);

        model.addAttribute("sysRole",sysRole);

        return "sysRole/update";
    }


    // 修改信息
    @PostMapping("/upd")
    public String upd(SysRole sysRole) {

        int upd = sysRoleService.upd(sysRole);

        if (upd>0){
            return "redirect:/sys/role/list";
        }else {
            return null;
        }
    }
}
